package com.dhcc.bpm.modules.gateway.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.common.enums.ConfigType;
import com.dhcc.bpm.common.redisMessage.RedisMessageSender;
import com.dhcc.bpm.modules.gateway.entity.ResponseCode;
import com.dhcc.bpm.modules.gateway.service.IResponseCodeService;
import com.dhcc.bpm.modules.gateway.vo.ResponseCodeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 返回码管理表 控制器
 *
 * @author xiafayu
 * @since 2021-05-06
 */
@RestController
@AllArgsConstructor
@RequestMapping("bpm-gateway/responsecode")
@Api(value = "返回码管理表", tags = "返回码管理表接口")
public class ResponseCodeController extends BladeController {
 	private final static Logger log = LoggerFactory.getLogger(ResponseCodeController.class);
 	@Autowired
	private IResponseCodeService responseCodeService;
	@Autowired
	private RedisMessageSender redisMessageSender;

	/**
	 * 返回码详情
	 */
	@ApiLog("查看返回码详情")
	@Log(title = "查看返回码详情",businessType = BusinessType.SELECT)
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "返回码详情", notes = "传入responseCode")
	public R<ResponseCode> detail(ResponseCode responseCode) {
		ResponseCode detail = responseCodeService.getOne(Condition.getQueryWrapper(responseCode));
		return R.data(detail);
	}

	/**
	 * 分页 返回码管理表
	 */
	@ApiLog("查看返回码列表")
	@Log(title = "查看返回码列表",businessType = BusinessType.SELECT)
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入responseCode")
	public R<IPage<ResponseCode>> list(ResponseCode responseCode, Query query) {
		IPage<ResponseCode> pages = responseCodeService.page(Condition.getPage(query), Condition.getQueryWrapper(responseCode));
		return R.data(pages);
	}

	/**
	 * 自定义分页 返回码管理表
	 */
	@ApiLog("查看返回码列表")
	@Log(title = "查看返回码列表",businessType = BusinessType.SELECT)
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入responseCode")
	public R<IPage<ResponseCodeVO>> page(ResponseCodeVO responseCode, Query query) {
		IPage<ResponseCodeVO> pages = responseCodeService.selectResponseCodePage(Condition.getPage(query), responseCode);
		return R.data(pages);
	}

	/**
	 * 新增 返回码管理表
	 */
	@ApiLog("新增返回码")
	@Log(title = "新增返回码",businessType = BusinessType.INSERT)
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入responseCode")
	public R save(@Valid @RequestBody ResponseCode responseCode) {
		return R.status(responseCodeService.save(responseCode));
	}

	/**
	 * 修改 返回码管理表
	 */
	@ApiLog("修改返回码")
	@Log(title = "修改返回码",businessType = BusinessType.UPDATE)
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入responseCode")
	public R update(@Valid @RequestBody ResponseCode responseCode) {
		return R.status(responseCodeService.updateById(responseCode));
	}

	/**
	 * 新增或修改 返回码管理表
	 */
	@ApiLog("新增或修改返回码")
	@Log(title = "新增或修改返回码",businessType = BusinessType.INSERT)
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入responseCode")
	public R submit(@Valid @RequestBody ResponseCode responseCode) {
		return R.status(responseCodeService.saveOrUpdate(responseCode));
	}

	
	/**
	 * 删除 返回码管理表
	 */
	@ApiLog("删除返回码")
	@Log(title = "删除返回码",businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(responseCodeService.removeByIds(Func.toLongList(ids)));
	}

	/**
	 * @Description 刷新返回码
	 * @Date 2019/11/27 15:02
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.gateway.entity.GatewayRoute>
	 **/
	@ApiLog("刷新返回码")
	@PostMapping("/refreshResponseCode")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "刷新返回码", notes = "传入")
	@Log(title = "刷新返回码", businessType = BusinessType.REFRESH)
	public R refreshResponseCode() {
		try {
			String message = redisMessageSender.format(ConfigType.RESPONSECODE.name(), "刷新返回码信息");
			redisMessageSender.sendMessageToGateway(null, message);

			int result = responseCodeService.updateRefreshStatus();
			if(result>=0){
				return R.status(true);
			}
			return R.status(false);
		}catch (Exception e){
			log.error("刷新返回码失败:{}", e);
			return R.status(false);
		}
	}
	
}
